package m2;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.*;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

public class Consumer {
    public static void main(String[] args) throws Exception {
        //创建消费者对象
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer");
        //设置name server
        consumer.setNamesrvAddr("192.168.64.141:9876");
        //设置从哪里订阅消息
        consumer.subscribe("Topic", "TagA||TagB");
        //注册消息监听器[registerMessageListener]
        consumer.registerMessageListener(new MessageListenerOrderly() {

            @Override
            public ConsumeOrderlyStatus consumeMessage(List<MessageExt> list, ConsumeOrderlyContext consumeOrderlyContext) {
                String t = Thread.currentThread().getName();
                for (MessageExt msg : list) {
                    System.out.println(t+" - "+ msg.getQueueId() + " - " +new String(msg.getBody()));

                }
                return ConsumeOrderlyStatus.SUCCESS;
            }
        });
        //启动
        consumer.start();
    }
}
